//
//  KongkaViewController.m
//  BMProject
//
//  Created by chris on 16/1/2.
//  Copyright © 2016年 BMProject. All rights reserved.
//

#import "KongkaViewController.h"
#import "HaokaBottomCell.h"
#import "KonghaoTopCell.h"
#import "DataModel.h"
#import "DitchManageChangeVc.h"
#import "SelectDeliveryMethyVc.h"
#import "UIViewController+common.h"

@interface KongkaViewController ()<UITableViewDelegate,UITableViewDataSource> {
    OrderDealDetailOrderInfo       *_orderInfo;
    OrderDealDetailOrderContentInfo*_contentInfo;
    UITableView * _tableview;
    KonghaoTopCell * _konghaoTopCell;
}
@end

@implementation KongkaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"工单信息确认";
    UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    tableview.delegate = self;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableview.sectionHeaderHeight = 8.0;
    tableview.sectionFooterHeight = 8.0;
    tableview.dataSource = self;
    tableview.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 10)];
    _tableview = tableview;
    [self addSubview:tableview];
    [self requestDetail];
}
#pragma mark-UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        static NSString * cellId = @"KonghaoTopCell";
        KonghaoTopCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell)
        {
            cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.vc = self;
        [cell setInfo:_model];
        [cell setOrderContentInfo:_contentInfo];
        if(_model.sign.intValue != 0 || _type == ORDER_LOOK_TYPE) {//不是确认状态或是详情页面
            cell.shijiLa.userInteractionEnabled = NO;
        }
        _konghaoTopCell = cell;
        return cell;
    }
    else
    {
        static NSString * cellId = @"HaokaBottomCell";
        HaokaBottomCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell)
        {
            cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell setOrderContentInfo:_contentInfo];
        return cell;
    }
}
#pragma mark-UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 180;
    }
    else
    {
        return 90*ratio_heigth;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 1)
    {
        return 50;
    }
    else
        return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if(_type == ORDER_LOOK_TYPE){
        return nil;
    }
    if (section == 1)
    {

        UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 1, DEF_SCREEN_WIDTH, 50)];
        footer.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
        
        UIButton * sureBtn = [UIButton buttonWithType:0];
        sureBtn.frame = CGRectMake(DEF_SCREEN_WIDTH/2-50, 10, 100, 30);
        sureBtn.layer.cornerRadius = 5.0;
        sureBtn.layer.masksToBounds = YES;
        sureBtn.backgroundColor = [UIColor orangeColor];
        sureBtn.titleLabel.font = font(13);
        if(_model.sign.intValue == 0){
            [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
        }else if(_model.sign.intValue == 1){
            [sureBtn setTitle:@"发货" forState:UIControlStateNormal];
        }else{
            [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
        }

        [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [sureBtn addTarget:self action:@selector(confimAction:) forControlEvents:UIControlEventTouchUpInside];
        [footer addSubview:sureBtn];
        
        return footer;
    }
    return nil;
}

#pragma mark 确认按钮事件
- (void)confimAction:(UIButton*)sender {
    if(_model.sign.intValue == 0){//确认状态
        if (_konghaoTopCell.shijiLa.text == nil || _konghaoTopCell.shijiLa.text.length <= 0 ) {
            [SVProgressHUD showErrorWithStatus:@"请输入实发数量"];
            return;
        }
        [UserServices orderAuditConfirmSjAmoutByToken:[[AppDelegate appDelegate] getUserInfo].token
                                                   id:_contentInfo.id
                                              basicId:_orderInfo.id
                                             sjAmount:_konghaoTopCell.shijiLa.text
                                           showDialog:YES
                                      completionBlock:^(int result, id responseObject) {
                                          if (result == API_Request_Success) {
                                              NSDictionary *responseDict = responseObject[0];
                                              NSString *poolWarning = [responseDict objectForKey:@"poolWarning"];
                                              if (!IsStringEmpty(poolWarning)) {
                                                  [SVProgressHUD showErrorWithStatus:poolWarning];
                                              }
                                              [self backButtonClick:nil];
                                          }
                                      }];
    }else if(_model.sign.intValue == 1){
        [UserServices orderAuditOptByToken:[[AppDelegate appDelegate] getUserInfo].token
                                   basicId:_model.id
                                       opt:@"fahuo"
                               expressName:@""
                                expressNum:@""
                             expressRemark:@""
                                  sendType:@""
                                showDialog:YES
                           completionBlock:^(int result, id responseObject) {
                               SelectDeliveryMethyVc *selectDeliveryMethyVc = [[SelectDeliveryMethyVc alloc] init];
                               selectDeliveryMethyVc.model = _model;
                               [self.navigationController pushViewController:selectDeliveryMethyVc animated:YES];
                           }];

    }else{
    }


}

- (void) requestDetail {
    //Type=1 类型为空卡工单时的返回报文
    if(_model.sign.intValue == 0){//确认状态
        [UserServices orderAuditDetailByToen:[[AppDelegate appDelegate] getUserInfo].token
                                          id:_model.id
                                        sign:_model.sign
                                        type:_model.type
                                  showDialog:YES
                             completionBlock:^(int result, id responseObject) {
                                 if (result != API_COULD_NOT_CONNECT_ERROR) {
                                     NSDictionary *responseDict = responseObject[0];
                                     NSDictionary *orderInfoDic        = [responseDict objectForKey:@"orderInfo"];
                                     NSDictionary *orderContentInfoDic = [responseDict objectForKey:@"orderContentInfo"];
                                     NSMutableArray *poolList          = [responseDict objectForKey:@"poolList"];//数组
                                     _orderInfo = [[OrderDealDetailOrderInfo alloc] initWithDictionary:orderInfoDic error:nil];
                                     _contentInfo = [[OrderDealDetailOrderContentInfo alloc] initWithDictionary:orderContentInfoDic error:nil];
                                     [_tableview reloadData];
                                 }
                             }];
    }else{
        [UserServices orderAuditFahuoByToen:[[AppDelegate appDelegate] getUserInfo].token
                                         id:_model.id
                                 showDialog:YES
                            completionBlock:^(int result, id responseObject) {
                                if (result != API_COULD_NOT_CONNECT_ERROR) {
                                    NSDictionary *responseDict = responseObject[0];
                                    NSDictionary *orderInfoDic        = [responseDict objectForKey:@"orderInfo"];
                                    NSDictionary *orderContentInfoDic = [responseDict objectForKey:@"orderContentInfo"];
                                    NSMutableArray *poolList          = [responseDict objectForKey:@"poolList"];//数组
                                    _orderInfo = [[OrderDealDetailOrderInfo alloc] initWithDictionary:orderInfoDic error:nil];
                                    _contentInfo = [[OrderDealDetailOrderContentInfo alloc] initWithDictionary:orderContentInfoDic error:nil];
                                    [_tableview reloadData];
                                }
                            }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
